/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JFrameDevRenov.java
 *
 * Created on 24/06/2011, 17:33:08
 */
package br.unesp.demac.bcc.soo.clarus.view;

import br.unesp.demac.bcc.soo.clarus.bean.Emprestimo;
import br.unesp.demac.bcc.soo.clarus.bean.Obra;
import br.unesp.demac.bcc.soo.clarus.dao.EmprestimoDAO;
import br.unesp.demac.bcc.soo.clarus.dao.EmprestimoDAOImp;
import br.unesp.demac.bcc.soo.clarus.util.DateUtil;
import br.unesp.demac.bcc.soo.clarus.util.FrameUtil;
import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrador
 */
public class JFrameDevRenov extends javax.swing.JFrame {

    /** Creates new form JFrameDevRenov */
    public JFrameDevRenov() {
        initComponents();
        FrameUtil.centralizar(this);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        txtIdObra = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnBuscar = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        btnRenovar = new javax.swing.JButton();
        btnDevolver = new javax.swing.JButton();
        txtNomeObra = new javax.swing.JTextField();
        txtAutores = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtDataRetirada = new javax.swing.JTextField();
        txtRA = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtNomeAluno = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtObs = new javax.swing.JLabel();
        jSeparator2 = new javax.swing.JSeparator();
        btnSubmit = new javax.swing.JButton();
        btnLimpar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Devolução e Renovação");
        setResizable(false);

        jLabel1.setFont(new java.awt.Font("Calibri", 1, 18));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/unesp/demac/bcc/soo/clarus/imagens/icon_devol.png"))); // NOI18N
        jLabel1.setText("Devolução e Renovação");

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Identificação da Obra");

        btnBuscar.setText("Buscar");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Informacoes", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 12))); // NOI18N

        btnRenovar.setText("Renovar");
        btnRenovar.setEnabled(false);
        btnRenovar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRenovarActionPerformed(evt);
            }
        });

        btnDevolver.setText("Devolver");
        btnDevolver.setEnabled(false);
        btnDevolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDevolverActionPerformed(evt);
            }
        });

        txtNomeObra.setEditable(false);

        txtAutores.setEditable(false);
        txtAutores.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtAutoresActionPerformed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Nome da Obra");

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("Autores");

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Data de Retirda");

        txtDataRetirada.setEditable(false);

        txtRA.setEditable(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel6.setText("RA");

        txtNomeAluno.setEditable(false);

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Nome do Aluno");

        txtObs.setFont(new java.awt.Font("Tahoma", 0, 10));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnDevolver, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnRenovar, javax.swing.GroupLayout.DEFAULT_SIZE, 323, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(48, 48, 48)
                        .addComponent(jLabel3)
                        .addGap(16, 16, 16)
                        .addComponent(txtNomeObra, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(txtAutores, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtRA, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtDataRetirada, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 139, Short.MAX_VALUE))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jLabel7)
                        .addGap(18, 18, 18)
                        .addComponent(txtNomeAluno, javax.swing.GroupLayout.DEFAULT_SIZE, 523, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addContainerGap(666, Short.MAX_VALUE)
                        .addComponent(txtObs)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomeObra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtAutores, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtDataRetirada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtRA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomeAluno, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel7))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
                .addComponent(txtObs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnRenovar, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnDevolver, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btnSubmit.setText("Finalizar");
        btnSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSubmitActionPerformed(evt);
            }
        });

        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(485, Short.MAX_VALUE))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(txtIdObra, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnBuscar)
                .addContainerGap(352, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(550, Short.MAX_VALUE)
                .addComponent(btnLimpar)
                .addGap(18, 18, 18)
                .addComponent(btnSubmit)
                .addContainerGap())
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtIdObra, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(btnBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSubmit)
                    .addComponent(btnLimpar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtAutoresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtAutoresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtAutoresActionPerformed

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        EmprestimoDAO edao = new EmprestimoDAOImp();
        try {
            currentEmprestimo = edao.getUltimoEmprestimoByIdObra(Integer.parseInt(txtIdObra.getText().trim()));
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, 
                    "Erro ao acessar o banco de dados", 
                    "Alerta", 
                    JOptionPane.WARNING_MESSAGE);
        }
        toggleOptions(false);
        txtObs.setText(null);
        if (currentEmprestimo != null) {
            Obra obra = currentEmprestimo.getObra().get(0);
            txtNomeObra.setText(obra.getNomeObra());
            txtAutores.setText(obra.autoresToString());
            txtDataRetirada.setText(DateUtil.formatDMY(currentEmprestimo.getDataEmprestimo()));
            txtRA.setText(((Long)currentEmprestimo.getAluno().getRa()).toString());
            txtNomeAluno.setText(currentEmprestimo.getAluno().getNome());
            
            Calendar hoje = Calendar.getInstance();
            hoje.add(Calendar.DATE, -1);
            if ((currentEmprestimo.getDataDevolucao() == null ) && currentEmprestimo.getDataEmprestimo().before(new Date(hoje.getTimeInMillis()))) {
                toggleOptions(true);
            } else {
                btnDevolver.setEnabled(true);
                txtObs.setText("Observação: A renovação somente poderá ser realizada 24 horas após o emprestimo.");
            }
        } else {
            JOptionPane.showMessageDialog(this,
                    "Emprestimo não encontrado",
                    "Alerta",
                    JOptionPane.WARNING_MESSAGE);
            resetFields();
        }
    }//GEN-LAST:event_btnBuscarActionPerformed

    private void btnDevolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDevolverActionPerformed
        boolean status = false;
        EmprestimoDAO edao = new EmprestimoDAOImp();
        try {
            status = edao.devolverEmprestimo(currentEmprestimo);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, 
                    "Erro ao acessar o banco de dados", 
                    "Alerta", 
                    JOptionPane.WARNING_MESSAGE);
        }
        
        if (status) {
            JOptionPane.showMessageDialog(this, 
                "Devolução realizada com sucesso!", 
                "Alerta", 
                JOptionPane.INFORMATION_MESSAGE);
            resetFields();
        } else {
            JOptionPane.showMessageDialog(this, 
                "Falha ao registrar devolução!", 
                "Alerta", 
                JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnDevolverActionPerformed

    private void btnRenovarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRenovarActionPerformed
        boolean status = false;
        EmprestimoDAO edao = new EmprestimoDAOImp();
        try {
            status = edao.renovarEmprestimo(currentEmprestimo);
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, 
                    "Erro ao acessar o banco de dados", 
                    "Alerta", 
                    JOptionPane.WARNING_MESSAGE);
        }
        
        if (status) {
            JOptionPane.showMessageDialog(this, 
                "Emprestimo renovado com sucesso!", 
                "Alerta", 
                JOptionPane.INFORMATION_MESSAGE);
            resetFields();
        } else {
            JOptionPane.showMessageDialog(this, 
                "Falha ao registrar renovação!", 
                "Alerta", 
                JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btnRenovarActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        resetFields();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void btnSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSubmitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnSubmitActionPerformed

    private void resetFields() {
        txtNomeObra.setText(null);
        txtAutores.setText(null);
        txtDataRetirada.setText(null);
        txtRA.setText(null);
        txtNomeAluno.setText(null);
        txtObs.setText(null);
        toggleOptions(false);
    }
    
    private void toggleOptions(boolean st) {
        btnDevolver.setEnabled(st);
        btnRenovar.setEnabled(st);
    }
        
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameDevRenov().setVisible(true);
            }
        });
    }
    
    private Emprestimo currentEmprestimo = null;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnDevolver;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnRenovar;
    private javax.swing.JButton btnSubmit;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTextField txtAutores;
    private javax.swing.JTextField txtDataRetirada;
    private javax.swing.JTextField txtIdObra;
    private javax.swing.JTextField txtNomeAluno;
    private javax.swing.JTextField txtNomeObra;
    private javax.swing.JLabel txtObs;
    private javax.swing.JTextField txtRA;
    // End of variables declaration//GEN-END:variables
}
